﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraVerticalGrid;
using System.Drawing;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;

namespace Sunboxsoft.ASMS.Client.UI.Common
{
    public static class StatusRender
    {
        /// <summary>
        /// 渲染维修单的状态
        /// </summary>
        /// <param name="e"></param>
        /// <param name="bill"></param>
        public static void RenderMaintenanceBillStatus(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e, WebRef.VMaintenanceBill bill)
        {
            Graphics graphics = e.Graphics;

            int posX = e.Bounds.Left + 1;
            int posY = e.Bounds.Top + 1;

            SizeF fontSize = graphics.MeasureString("派", e.Appearance.Font);
            //
            int cellWidth = 20;
            int cellHeight = e.Bounds.Height - 2;
            int arrowWidth = 3;

            int fontDeltaX = (int)((cellWidth - fontSize.Width) / 2);
            int fontDeltaY = (int)((cellHeight - fontSize.Height) / 2);


            Brush[] fillBrushArray = new Brush[7];
            if (bill.Status == Domain.Constant.MaintenanceBillStatus.WaitDispatching)
            {
                // 等待派工
                fillBrushArray[0] = Brushes.LightGray;
                fillBrushArray[1] = Brushes.LightGray;
                fillBrushArray[2] = Brushes.LightGray;
                fillBrushArray[3] = Brushes.LightGray;
                fillBrushArray[4] = Brushes.LightGray;
                fillBrushArray[5] = Brushes.LightGray;
                fillBrushArray[6] = Brushes.LightGray;
            }
            else if (bill.Status == Domain.Constant.MaintenanceBillStatus.Working)
            {
                // 施工中
                if (bill.ServiceItemStatus == Domain.Constant.MaintenanceBillStatus.ServiceItemStatus.Wait
                    || bill.ServiceItemStatus == Domain.Constant.MaintenanceBillStatus.ServiceItemStatus.PartialWait)
                {
                    fillBrushArray[0] = Brushes.LightGray;
                    fillBrushArray[2] = Brushes.LightGray;
                }
                else if (bill.ServiceItemStatus == Domain.Constant.MaintenanceBillStatus.ServiceItemStatus.Working
                    || bill.ServiceItemStatus == Domain.Constant.MaintenanceBillStatus.ServiceItemStatus.PartialWorking)
                {
                    fillBrushArray[0] = Brushes.Green;
                    fillBrushArray[2] = Brushes.LightGray;
                }
                else
                {
                    fillBrushArray[0] = Brushes.Green;
                    fillBrushArray[2] = Brushes.Green;
                }

                if (bill.MaterialStatus != Domain.Constant.MaintenanceBillStatus.MaterialStatus.Finished)
                {
                    fillBrushArray[1] = Brushes.LightGray;
                }
                else
                {
                    fillBrushArray[1] = Brushes.Green;
                }
                fillBrushArray[3] = Brushes.LightGray;
                fillBrushArray[4] = Brushes.LightGray;
                fillBrushArray[5] = Brushes.LightGray;
            }
            else if (bill.Status == Domain.Constant.MaintenanceBillStatus.WaitSettling)
            {
                // 已竣工，等待结算
                fillBrushArray[0] = Brushes.Green;
                fillBrushArray[1] = Brushes.Green;
                fillBrushArray[2] = Brushes.Green;
                fillBrushArray[3] = Brushes.LightGray;
                fillBrushArray[4] = Brushes.LightGray;
                fillBrushArray[5] = Brushes.LightGray;
            }
            else if (bill.Status == Domain.Constant.MaintenanceBillStatus.WaitCheckOut)
            {
                // 已结算，等待收银
                fillBrushArray[0] = Brushes.Green;
                fillBrushArray[1] = Brushes.Green;
                fillBrushArray[2] = Brushes.Green;
                fillBrushArray[3] = Brushes.Green;
                fillBrushArray[4] = Brushes.LightGray;
                fillBrushArray[5] = Brushes.LightGray;
            }
            else if (bill.Status == Domain.Constant.MaintenanceBillStatus.WaitDelivery)
            {
                // 已收银，等待出厂
                fillBrushArray[0] = Brushes.Green;
                fillBrushArray[1] = Brushes.Green;
                fillBrushArray[2] = Brushes.Green;
                fillBrushArray[3] = Brushes.Green;
                fillBrushArray[4] = Brushes.Green;
                fillBrushArray[5] = Brushes.LightGray;
            }
            else if (bill.Status == Domain.Constant.MaintenanceBillStatus.Finished)
            {
                fillBrushArray[0] = Brushes.Green;
                fillBrushArray[1] = Brushes.Green;
                fillBrushArray[2] = Brushes.Green;
                fillBrushArray[3] = Brushes.Green;
                fillBrushArray[4] = Brushes.Green;
                fillBrushArray[5] = Brushes.Green;
            }
            // 0
            // *        *
            //            *
            // *        *
            graphics.FillPolygon(fillBrushArray[0], new Point[]{
                new Point(posX, posY),
                new Point(posX + cellWidth, posY),
                new Point(posX + cellWidth + arrowWidth, posY + cellHeight/ 2),
                new Point(posX + cellWidth, posY + cellHeight),
                new Point(posX, posY + cellHeight),
            });
            graphics.DrawString("派", e.Appearance.Font, Brushes.White, new PointF(posX + fontDeltaX, posY + fontDeltaY));

            // 1
            // *        *
            //   *        *
            // *        *
            posX += cellWidth + arrowWidth;
            graphics.FillPolygon(fillBrushArray[1], new Point[]{
                new Point(posX, posY),
                new Point(posX + cellWidth, posY),
                new Point(posX + cellWidth + arrowWidth, posY + cellHeight/ 2),
                new Point(posX + cellWidth, posY + cellHeight),
                new Point(posX, posY + cellHeight),
                new Point(posX + arrowWidth, posY + cellHeight / 2),
            });
            graphics.DrawString("领", e.Appearance.Font, Brushes.White, new PointF(posX + arrowWidth + fontDeltaX, posY + fontDeltaY));

            // 2
            // *        *
            //   *        *
            // *        *
            posX += cellWidth + arrowWidth;
            graphics.FillPolygon(fillBrushArray[2], new Point[]{
                new Point(posX, posY),
                new Point(posX + cellWidth, posY),
                new Point(posX + cellWidth + arrowWidth, posY + cellHeight/ 2),
                new Point(posX + cellWidth, posY + cellHeight),
                new Point(posX, posY + cellHeight),
                new Point(posX + arrowWidth, posY + cellHeight / 2),
            });
            graphics.DrawString("竣", e.Appearance.Font, Brushes.White, new PointF(posX + arrowWidth + fontDeltaX, posY + fontDeltaY));

            // 3
            // *        *
            //   *        *
            // *        *
            posX += cellWidth + arrowWidth;
            graphics.FillPolygon(fillBrushArray[3], new Point[]{
                new Point(posX, posY),
                new Point(posX + cellWidth, posY),
                new Point(posX + cellWidth + arrowWidth, posY + cellHeight/ 2),
                new Point(posX + cellWidth, posY + cellHeight),
                new Point(posX, posY + cellHeight),
                new Point(posX + arrowWidth, posY + cellHeight / 2),
            });
            graphics.DrawString("结", e.Appearance.Font, Brushes.White, new PointF(posX + arrowWidth + fontDeltaX, posY + fontDeltaY));

            // 4
            // *        *
            //   *        *
            // *        *
            posX += cellWidth + arrowWidth;
            graphics.FillPolygon(fillBrushArray[4], new Point[]{
                new Point(posX, posY),
                new Point(posX + cellWidth, posY),
                new Point(posX + cellWidth + arrowWidth, posY + cellHeight/ 2),
                new Point(posX + cellWidth, posY + cellHeight),
                new Point(posX, posY + cellHeight),
                new Point(posX + arrowWidth, posY + cellHeight / 2),
            });
            graphics.DrawString("银", e.Appearance.Font, Brushes.White, new PointF(posX + arrowWidth + fontDeltaX, posY + fontDeltaY));

            // 5
            // *        *
            //   *        *
            // *        *
            posX += cellWidth + arrowWidth;
            graphics.FillPolygon(fillBrushArray[5], new Point[]{
                new Point(posX, posY),
                new Point(posX + cellWidth, posY),
                new Point(posX + cellWidth + arrowWidth, posY + cellHeight/ 2),
                new Point(posX + cellWidth, posY + cellHeight),
                new Point(posX, posY + cellHeight),
                new Point(posX + arrowWidth, posY + cellHeight / 2),
            });
            graphics.DrawString("出", e.Appearance.Font, Brushes.White, new PointF(posX + arrowWidth + fontDeltaX, posY + fontDeltaY));
        }
    }
}
